package uit.mmt02.activity;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Message;

import uit.mmt02.R;
import uit.mmt02.share.ConvertMessageToList;
import uit.mmt02.share.OneMail;
import uit.mmt02.share.myadapter;
import uit.mmt02.share.showToast;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class getYahooActivity extends ListActivity
{
	final static String KEY_USERNAME_LOGIN = "USERNAMELOGIN"; // nhan USERVA PASS tu showmail activity gui toi de login
	final static String KEY_PASSWORD_LOGIN = "PASSWORDLOGIN";
	static final String FROM = "from";
	static final String BODY = "body";
	static final String SUB = "sub";
	static final String DATE = "date";
	static final String TIME = "time";
	static final String TO = "to";
	String username = "sonntuit";
	String password = "sacrifices";
	ArrayAdapter<OneMail>adapter;
	Message[]message;
	showToast d;
	List<OneMail>list;
	int n ; // so mail
	String tag   = "LOG YAHOOMAIL";
	Bundle bundle;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		bundle = getIntent().getExtras();
		username = bundle.getString(KEY_USERNAME_LOGIN);
		password = bundle.getString(KEY_PASSWORD_LOGIN);
		d = new showToast(getBaseContext());
		list = new ArrayList<OneMail>();		
		new loadgmail().execute("yahoomail");
	}
	
	// asyncTask Load mail
		private class loadgmail extends AsyncTask<String,String	, Message[]>
		{
			uit.mmt02.mailControl.YahooControl g ;
			ProgressDialog pdialog = new ProgressDialog(getYahooActivity.this);
			
			@Override
			protected void onPreExecute() 
			{
				pdialog.setTitle("Get Mail");
				pdialog.setMessage("Loading mail...");
				pdialog.show();
				// TODO Auto-generated method stub
				super.onPreExecute();
			}

			@Override
			protected Message[] doInBackground(String... params) 
			{
				// TODO Auto-generated method stub
				Log.v(tag, "doinBackGroud");
				g = new uit.mmt02.mailControl.YahooControl(username, password);
				message = g.getmail();
				Log.i(tag, "get thanh cong yahoo");
				ConvertMessageToList cv = new ConvertMessageToList();
				list = cv.convert2(message);
				adapter = new myadapter(getYahooActivity.this, list);
				Log.v(tag, "end doinBackGroud");
				return message;
				
			}
			@Override
			protected void onPostExecute(Message[] resultMes) {
				// TODO Auto-generated method stub
				Log.v("mail","on post");
				
				setListAdapter(adapter); // hien thi list view
				pdialog.dismiss(); // huy progress dialog
				super.onPostExecute(resultMes);
			}
			
		}
		
		//click doc noi dung mail
		@Override
		protected void onListItemClick(ListView l, View v, int position, long id) 
		{
			// TODO Auto-generated method stub
			super.onListItemClick(l, v, position, id);
			d.showmess("read mail");
			
//			Intent i = new Intent("uit.mmt02.GBODY");
			Intent i = new Intent(this, uit.mmt02.activity.readmailActivity.class);
//			OneMail om1 = listmail.get(position);
//			i.putExtra(FROM, om1.getFrom());
//			i.putExtra(SUB, om1.getSub());
//			i.putExtra(BODY, om1.getBody());
//			i.putExtra(DATE, om1.getDateSend());
			i.putExtra(FROM, "nung286@gmail.com");
			i.putExtra(SUB, "android mail check");
			i.putExtra(BODY, "toi la nguyen thanh son\n chao chu sonnt\n lalala");
			i.putExtra(DATE, "02/11/2011");
			i.putExtra(TO, "sonntuit@gmail.com");
			Log.v("putExtra", "put xong");
			
			startActivity(i);

			
		}
		
		//menu
		// tao menu 
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// TODO Auto-generated method stub
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.layout_menu, menu);
			return super.onCreateOptionsMenu(menu);
		}
		//khi click vao menu item
		@Override
		public boolean onOptionsItemSelected(MenuItem item) 
		{
			// TODO Auto-generated method stub
			switch (item.getItemId()) 
			{
			case R.id.item_newmail:
				d.showmess("new mail");
				Intent i = new Intent("uit.mmt02.activity.newmailActivity");
				startActivity(i);
				break;
			case R.id.item_sortby:
				d.showmess("sort by");
				break;
			case R.id.item_selectAll:
				d.showmess("selected all");
				break;
			case R.id.item_check:
				d.showmess("check mail again");
				break;
			case R.id.item_more:
				d.showmess("more");
				break;

			default:
				break;
			}
			return super.onOptionsItemSelected(item);
		}

}
